package cn.iocoder.yudao.module.personal.job;

import cn.hutool.http.HttpUtil;
import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
import cn.iocoder.yudao.module.system.api.sms.SmsSendApi;
import cn.iocoder.yudao.module.system.api.sms.dto.send.SmsSendSingleToUserReqDTO;
import com.alibaba.excel.util.StringUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author: huqngqi
 * @date: 2024/12/2 17:33
 * @description: 天气预报提醒服务
 */
@Component
@Slf4j
public class WeatherForecastReminderJob implements JobHandler {

    @Resource
    private SmsSendApi smsSendApi;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public String execute(String param) throws Exception {
        if (StringUtils.isBlank(param)) {
            return "入参为空";
        }
        JSONArray paramJsonArray = JSONArray.parseArray(param);
        for(int i = 0; i < paramJsonArray.size(); i++){
            JSONObject jsonObject = paramJsonArray.getJSONObject(i);
            String mobiles = jsonObject.getString("mobile");
            if (StringUtils.isBlank(mobiles)) {
                continue;
            }
            Long channelId = jsonObject.getLong("channelId");
            String templateCode = jsonObject.getString("templateCode");
            Map<String, Object> templateParams = jsonObject.getJSONObject("templateParams");
            if(templateParams == null){
                templateParams = new HashMap<>();
            }
            //实时天气 101120111-历城 101120910-沂水
            String location = jsonObject.getString("location");
            HashMap<String, Object> paramMap = new HashMap<>();
            paramMap.put("location", location);
            paramMap.put("key", "00876934f1a54a0b9a97e05007f30555");
            String result = HttpUtil.get("https://api.qweather.com/v7/weather/3d", paramMap);
            log.info("result:{}", result);
            if (StringUtils.isBlank(result)) {
                return "天气预报提醒服务异常";
            }
            JSONObject jsonResult = JSONObject.parseObject(result);
            if (!"200".equals(jsonResult.get("code"))) {
                return "天气预报提醒服务异常";
            }
            /*
             * 今天(${todayStr}): 气温${qwToday},白天${textDayToday},晚间${textNightToday}
             * 明天(${tomorrowStr}): 气温${qwTomorrow},白天${textDayTomorrow},晚间${textNightTomorrow}
             * 后天(${afterStr}): 气温${qwAfter},白天${textDayAfter},晚间${textNightAfter}
             */
            JSONArray dailyJsonArray = jsonResult.getJSONArray("daily");
            for (int j = 0; j < dailyJsonArray.size(); j++) {
                JSONObject dailyJsonObject = dailyJsonArray.getJSONObject(j);
                String fxDate = dailyJsonObject.getString("fxDate");
                String sunset = dailyJsonObject.getString("sunset");
                String tempMax = dailyJsonObject.getString("tempMax");
                String tempMin = dailyJsonObject.getString("tempMin");
                String windDirDay = dailyJsonObject.getString("windDirDay");
                String windScaleDay = dailyJsonObject.getString("windScaleDay");
                String textDay = dailyJsonObject.getString("textDay");
                String textNight = dailyJsonObject.getString("textNight");
                if(j==1){
                    templateParams.put("tomorrowStr", fxDate);
                    templateParams.put("qwTomorrow", tempMax +"℃ ~ " + tempMin +"℃");
                    templateParams.put("textDayTomorrow", textDay);
                    templateParams.put("textNightTomorrow", textNight +","+sunset+"日落,"+windDirDay+windScaleDay+"级 ");
                }else if(j==0){
                    templateParams.put("todayStr", fxDate);
                    templateParams.put("qwToday", tempMax +"℃ ~ " + tempMin +"℃");
                    templateParams.put("textDayToday", textDay);
                    templateParams.put("textNightToday", textNight +","+sunset+"日落,"+windDirDay+windScaleDay+"级 ");
                }else if(j==2){
                    templateParams.put("afterStr", fxDate);
                    templateParams.put("qwAfter", tempMax +"℃ ~ " + tempMin +"℃");
                    templateParams.put("textDayAfter", textDay);
                    templateParams.put("textNightAfter", textNight +","+sunset+"日落,"+windDirDay+windScaleDay+"级 ");
                }

                stringRedisTemplate.opsForValue().set("weather_warning_today", tempMax +"℃ ~ " + tempMin +"℃", 1, TimeUnit.DAYS);
            }
            log.info("templateParams:{}", JSONObject.toJSONString(templateParams));
            String[] mobilesAttr = mobiles.split(",");
            for (String mobile : mobilesAttr) {
                SmsSendSingleToUserReqDTO reqDTO = new SmsSendSingleToUserReqDTO();
                reqDTO.setMobile(mobile);
                reqDTO.setTemplateCode(templateCode);
                reqDTO.setTemplateParams(templateParams);
                reqDTO.setChannelId(channelId);
                smsSendApi.sendSingleSmsDirectToMobile(reqDTO);
            }
        }
        return null;
    }
}
